package io.zhengsh.juc._4forkjoin;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

public class CompletableFutureTest {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        // 无返回值
        Runnable runnable = () -> System.out.println("执行无返回结果的异步任务");
        CompletableFuture.runAsync(runnable);

        // 有返回值
        CompletableFuture<String> completableFuture =
                CompletableFuture.supplyAsync(() -> {
                    System.out.println("执行有返回值的异步任务");
                    try {
                        TimeUnit.SECONDS.sleep(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    return "Hello World!";
                });
        //String o = completableFuture.get();
        String o = completableFuture.join();
        System.out.println(o);

    }
}
